home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_2 / vsrc.tar / voyager7_src / memory.c < prev    next >
C/C++ Source or Header  |  1991-02-27  |  5KB  |  240 lines

  1. /*
  2. // Abstract:
  3. //    MEMORY---Memory Operations
  4. //
  5. //    The Memory Operations module provides access to the HP 48SX's
  6. //    memory, as stored in the memory dump.
  7. //
  8. // Author:
  9. //    Derek S. Nickel
  10. //
  11. // Creation date:
  12. //    28 October 1990
  13. //
  14. // History:
  15. // V01-001    Derek S. Nickel        28-OCT-1990
  16. //    Original.
  17. //
  18. */
  19.  
  20. #include <string.h>
  21. #include <stdlib.h>
  22.  
  23. #include "index.h"
  24. #include "memory.h"
  25. #include "pager.h"
  26. #include "instruct.h"
  27.  
  28. static TextFile port0_ca_file;
  29. static TextFile port0_cc_file;
  30. static TextFile port1_ca_file;
  31. static TextFile port1_cc_file;
  32. static TextFile port2_ca_file;
  33. static TextFile port2_cc_file;
  34.  
  35. static char port0_module[_MAX_PATH];
  36. static char port1_module[_MAX_PATH];
  37. static char port2_module[_MAX_PATH];
  38.  
  39. port_info_t ports[3] = {
  40.   0, 0, 0x00000, 0x7FFFF, port0_module, NULL, &port0_ca_file, &port0_cc_file,
  41.   0, 0, 0x80000, 0xBFFFF, port1_module, NULL, &port1_ca_file, &port1_cc_file,
  42.   0, 0, 0xC0000, 0xFFFFF, port2_module, NULL, &port2_ca_file, &port2_cc_file
  43. };
  44.  
  45. const int bad_port = -1;
  46. const bin5_t bad_address = 0x100000;
  47.  
  48. int WorkPort = -1;
  49. bin5_t WorkPtr = 0;
  50.  
  51. /***********************************************************************
  52.     Messages.
  53. ***********************************************************************/
  54.  
  55. #define vgr__errsetadr \
  56. "%%VOYAGER-E-ERRACCADR, error accessing address %05lX\n"
  57.  
  58. #define vgr__pornotloa \
  59. "-VOYAGER-E-PORNOTLOA, port %d has not been loaded\n"
  60.  
  61. /***********************************************************************
  62.     get_port_number
  63. ***********************************************************************/
  64.  
  65. static int get_port_number(bin5_t adr)
  66. {
  67.     int port_no;
  68.  
  69.     for (port_no = 0; port_no <= 2; port_no++) {
  70.         if (adr >= ports[port_no].min_adr &&
  71.             adr <= ports[port_no].max_adr) {
  72.             return port_no;
  73.         }
  74.     }
  75.  
  76.     return bad_port;
  77. }
  78.  
  79. /***********************************************************************
  80.     xlate_adr
  81. ***********************************************************************/
  82.  
  83. int xlate_adr(bin5_t adr, int *port_no, bin5_t *port_adr)
  84. {
  85.     int return_status;
  86.  
  87.     *port_no = get_port_number(adr);
  88.  
  89.     switch (*port_no) {
  90.         case 0:
  91.         case 1:
  92.         case 2:
  93.         *port_adr = adr - ports[*port_no].min_adr;
  94.         return_status = ports[*port_no].loaded;
  95.         break;
  96.  
  97.         default:
  98.         *port_adr = bad_address;
  99.         return_status = 0;
  100.         break;
  101.     }
  102.  
  103.     return return_status;
  104. }
  105.  
  106. /***********************************************************************
  107.     SetWorkPtr
  108. ***********************************************************************/
  109.  
  110. void SetWorkPtr(bin5_t adr)
  111. {
  112.     int port_no;
  113.     bin5_t port_adr;
  114.  
  115.     if (xlate_adr(adr, &port_no, &port_adr)) {
  116.         if (WorkPort != port_no || WorkPtr != adr) {
  117.             WorkPort = port_no;
  118.             WorkPtr = adr;
  119.             fseek(ports[port_no].mem_file, port_adr, SEEK_SET);
  120.         }
  121.  
  122.     } else {
  123.         pager(0);
  124.         printf(vgr__errsetadr, adr);
  125.         pager(0);
  126.         printf(vgr__pornotloa, port_no);
  127.     }
  128. }
  129.  
  130. /***********************************************************************
  131.     GetNibble
  132. ***********************************************************************/
  133.  
  134. char GetNibble(void)
  135. {
  136.     int port_no;
  137.     bin5_t port_adr;
  138.     int ans = 0;
  139.  
  140.     if (xlate_adr(WorkPtr, &port_no, &port_adr)) {
  141.         if (WorkPort != port_no) {
  142.             WorkPort = port_no;
  143.             fseek(ports[port_no].mem_file, port_adr, SEEK_SET);
  144.         }
  145.  
  146.         ans = getc(ports[port_no].mem_file);
  147.         if (ans != EOF) ++WorkPtr;
  148.     }
  149.  
  150.     return (char) ans;
  151. }
  152.  
  153. /***********************************************************************
  154.     GetNNibbles
  155. ***********************************************************************/
  156.  
  157. void GetNNibbles(char *ans, int n)
  158. {
  159.     int port_no;
  160.     bin5_t port_adr;
  161.     int numRead;
  162.  
  163.     if (xlate_adr(WorkPtr, &port_no, &port_adr)) {
  164.         if (WorkPort != port_no) {
  165.             WorkPort = port_no;
  166.             fseek(ports[port_no].mem_file, port_adr, SEEK_SET);
  167.         }
  168.  
  169.         numRead = fread(ans, sizeof(char), n, ports[port_no].mem_file);
  170.         ans[numRead] = '\0';
  171.         WorkPtr += numRead;
  172.     }
  173. }
  174.  
  175. /***********************************************************************
  176.     str2adr
  177. ***********************************************************************/
  178.  
  179. bin5_t str2adr(char *s, char **wp)
  180. {
  181.     bin5_t ans;
  182.     char *junk;
  183.  
  184.     ans = strtoul(s, (wp) ? wp : &junk, 16);
  185.  
  186.     if (ans > 0xFFFFF) {
  187.         puts("\n%*** address out of range ***");
  188.         ans = 0;
  189.     }
  190.  
  191.     return ans;
  192. }
  193.  
  194. /***********************************************************************
  195.     get_bin5
  196. ***********************************************************************/
  197.  
  198. bin5_t get_bin5(void)
  199. {
  200.     char buf[6];
  201.  
  202.     GetNNibbles(buf,5);
  203.     strrev(buf);
  204.     return str2adr(buf, 0);
  205. }
  206.  
  207. /***********************************************************************
  208.     get_address_comment
  209. ***********************************************************************/
  210.  
  211. char *get_address_comment(bin5_t adr)
  212. {
  213.     int port_no;
  214.     bin5_t port_adr;
  215.     static char zero = '\0';
  216.  
  217.     if (xlate_adr(adr, &port_no, &port_adr))
  218.         return GetTextLine(
  219.             ports[port_no].ca_file, port_adr);
  220.     else
  221.         return &zero;
  222. }
  223.  
  224. /***********************************************************************
  225.     get_code_comment
  226. ***********************************************************************/
  227.  
  228. char *get_code_comment(bin5_t adr)
  229. {
  230.     int port_no;
  231.     bin5_t port_adr;
  232.     static char zero = '\0';
  233.  
  234.     if (xlate_adr(adr, &port_no, &port_adr))
  235.         return GetTextLine(
  236.             ports[port_no].cc_file, port_adr);
  237.     else
  238.         return &zero;
  239. }
  240.